package com.eipc.util;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

import com.eipc.model.Game;
import com.eipc.model.ImportNotice;
import com.eipc.model.Department;
import com.eipc.model.Match;
import com.eipc.model.Person;
import com.eipc.model.Player;
import com.eipc.model.PlayerGroup;
import com.eipc.model.Position;
import com.eipc.model.Reply;
import com.eipc.model.ReplyResponse;
import com.eipc.model.Response;
import com.eipc.model.Score;
import com.eipc.model.SerInfoResponse;

/**
 * json解析
 * @author x_liaolijun
 *
 * 2010-9-2 下午07:25:29
 *  
 * JsonParse
 *
 */
public class JsonParse {
	
	/**
	 * 解析响应对数据
	 * @param str
	 * @return
	 */
	public static Response parseRespnse(String str){
		try {
			JSONObject jo = new JSONObject(str);
			Response resp = new Response();
			resp.failure = jo.getBoolean("failure");
			resp.success = jo.getBoolean("success");
			if(resp.success){
				resp.id = jo.getString("id");
			}
			return resp;
		} catch (JSONException e) {
			MyLog.error(JsonParse.class,"", e);
			return null;
		}
	}
	
	/**
	 * 解析响应对数据
	 * @param str
	 * @return
	 */
	public static ReplyResponse parseReplyRespnse(String str){
		try {
			JSONObject jo = new JSONObject(str);
			ReplyResponse resp = new ReplyResponse();
			resp.failure = jo.getBoolean("failure");
			resp.success = jo.getBoolean("success");
			if(resp.success){
				resp.id = jo.getString("id");
				JSONArray rjs = jo.getJSONArray("res");
				if(rjs!=null&&rjs.length()>0){
					Reply[] rs = new Reply[rjs.length()];
					for (int i=0;i<rjs.length();i++){
						Reply r = DataParse.ParseReply((String)rjs.get(i));
						r.nID = resp.id;
						rs[i] = r;
					}
					resp.rs = rs;
				}
			}
			return resp;
		} catch (JSONException e) {
			MyLog.error(JsonParse.class,"", e);
			return null;
		}
	}
	
	/**
	 * 解析响应对数据
	 * @param str
	 * @return
	 */
	public static SerInfoResponse parseSerInfoResponse(String str,int model){
		try {
			JSONObject jo = new JSONObject(str);
			SerInfoResponse resp = new SerInfoResponse();
			resp.failure = jo.getBoolean("failure");
			resp.success = jo.getBoolean("success");
			if(resp.success){
				JSONArray rjs = jo.getJSONArray("res");
				Object[] os = null;
				if(rjs!=null&&rjs.length()>0){
					switch(model){
						case 0:os = new Player[rjs.length()];
							 for (int i=0;i<rjs.length();i++){
								Player o = DataParse.ParsePlayer((String)rjs.get(i));
								os[i] = o;
							 }
							 break;
						case 1:os = new PlayerGroup[rjs.length()];
							for (int i=0;i<rjs.length();i++){
								PlayerGroup o = DataParse.ParsePlayerGroup((String)rjs.get(i));
								os[i] = o;
						}break;
						case 2:os = new Match[rjs.length()];
							for (int i=0;i<rjs.length();i++){
								Match o = DataParse.ParseMatch((String)rjs.get(i));
								os[i] = o;
							}break;
						case 3:os = new Score[rjs.length()];
					 		 for (int i=0;i<rjs.length();i++){
					 			Score o = DataParse.ParseScore((String)rjs.get(i));
								os[i] = o;
							 }break;
						case 4:os = new Game[rjs.length()];
						for (int i=0;i<rjs.length();i++){
							Game o = DataParse.ParseGame((String)rjs.get(i));
							os[i] = o;
						}break;
						case 5:resp.id = jo.getString("maxID");
						 os = new ImportNotice[rjs.length()];
						 Log.e("--------os len----------",String.valueOf(rjs.length()));
				 		 for (int i=0;i<rjs.length();i++){
				 			ImportNotice o = DataParse.ParseImportNotice((String)rjs.get(i));
							os[i] = o;
						 }break;
							 
					}
					resp.os = os;
				}
			}
			return resp;
		} catch (JSONException e) {
			MyLog.error(JsonParse.class,"", e);
			return null;
		}
	}
	
	/**
	 * 解析职位信息
	 * @param str
	 * @return
	 */
	public static ArrayList<Position> parsePosition(String str){
		try {
			ArrayList<Position> positions = new ArrayList<Position>();
			JSONObject jo = new JSONObject(str);
			JSONArray positionList = jo.getJSONArray("topics");
			if(positionList!=null){
				JSONObject position;
				Position p = null;
				int len = positionList.length();
				for(int i=0;i<len;i++){
					position = positionList.getJSONObject(i);
					p = new Position();
					p.positionId = position.getString("id");
					p.name = position.getString("name");
					positions.add(p);
				}
			}
			return positions;
		} catch (JSONException e) {
			MyLog.error(JsonParse.class,"", e);
			return null;
		}
	}
	
	/**
	 * 解析部门信息
	 * @param str
	 * @return
	 */
	public static ArrayList<Department> parseDepartment(String str){
		try {
			ArrayList<Department> departments = new ArrayList<Department>();
			JSONObject jo = new JSONObject(str);
			JSONArray departmentList = jo.getJSONArray("topics");
			if(departmentList!=null){
				JSONObject deparment;
				Department d = null;
				int len = departmentList.length();
				for(int i=0;i<len;i++){
					deparment = departmentList.getJSONObject(i);
					d = new Department();
					d.departmentId = deparment.getString("id");
					d.name = deparment.getString("name");
					departments.add(d);
				}
			}
			return departments;
		} catch (JSONException e) {
			MyLog.error(JsonParse.class,"", e);
			return null;
		}
	}
	
	/**
	 * 解析人员信息
	 * @param str
	 * @return
	 */
	public static ArrayList<Person> parsePerson(String str){
		try {
			ArrayList<Person> persons = new ArrayList<Person>();
			JSONObject jo = new JSONObject(str);
			JSONArray personList = jo.getJSONArray("topics");
			if(personList!=null){
				JSONObject person;
				JSONObject other;
				Person p = null;
				int len = personList.length();
				for(int i=0;i<len;i++){
					person = personList.getJSONObject(i);
					p = new Person();
					p.personId = person.getString("id");
					other = person.getJSONObject("department");
					p.departmentId = other.getString("id");
					other = person.getJSONObject("profession");
					p.positionId = other.getString("id");
					p.name = person.getString("name");
					p.mobile = person.getString("mobile");
					persons.add(p);
				}
			}
			return persons;
		} catch (JSONException e) {
			MyLog.error(JsonParse.class,"", e);
			return null;
		}
	}
	
	/**
	 * 解析指挥中心数据
	 * @param str
	 * @return
	 */
	public static ArrayList<ImportNotice> parseControlCenter(String str){
		try {
			ArrayList<ImportNotice> infos = new ArrayList<ImportNotice>();
			JSONObject jo = new JSONObject(str);
			JSONArray commandList = jo.getJSONArray("topics");
			if(commandList!=null){
				JSONObject command;
				JSONObject person;
				ImportNotice info = null;
				int len = commandList.length();
				for(int i=0;i<len;i++){
					command = commandList.getJSONObject(i);
					if(command!=null){
						info = new ImportNotice();
						info.noticeId = command.getInt("id");
						info.content = command.getString("content");
						person = command.getJSONObject("issuer");
						info.issuer = person.getString("id");
						info.issueTime = Tools.toDate(command.getString("issueTime"));
						person = command.getJSONObject("sender");
						info.sender = person.getString("id");
						info.sendTime = Tools.toDate(command.getString("sendTime"));
						info.sendType = command.getString("sendType");
						info.top = command.getString("top");
						infos.add(info);
					}
				}
			}
			return infos;
		} catch (JSONException e) {
			MyLog.error(JsonParse.class,"", e);
			return null;
		}
	}

}
